###########################################
# Figure 1: Share of California Local Tax Referendums Approved

###########################################


# Median chg.itm 
city$high_chg <- ifelse(city$chg_prop.itm >= .1389888,1,0)

high <- city[city$high_chg == 1,]

R <- lm(data = high,formula =  Pass ~ 0 + as.factor(year))
a <- as.data.frame(cbind(R$coefficients,confint(R,level = 0.9)))
colnames(a) <- c("perc_yes","lb","ub") 
a$year <- as.numeric(substr(rownames(a),16,19))

ggplot(data = a,aes(x=year,y=perc_yes))+
  geom_line()+
  geom_point()+
  geom_ribbon(aes(ymin = lb, ymax = ub), alpha = 0.1,fill = "grey",colour = NA)+
  theme_minimal()

R <- lm(data = df[df$highpropchange ==1,],formula =  Pass ~ 0 + as.factor(year))
b <- as.data.frame(cbind(R$coefficients,confint(R,level = 0.9)))
colnames(b) <- c("perc_yes","lb","ub") 
b$year <- as.numeric(substr(rownames(b),16,19))


ggplot(data = b,aes(x=year,y=perc_yes))+
  geom_line()+
  geom_point()+
  geom_ribbon(aes(ymin = lb, ymax = ub), alpha = 0.1,fill = "grey",colour = NA)+
  theme_minimal()

b$`Referendums in` <- "School districts"
a$`Referendums in` <- "Cities"

all <- rbind(a,b)

all <- all[all$year %%2 ==0,]

tcja <- data.frame(xmin= c(2018.5), 
                   xmax= c(2022.25), 
                   ymin=0.25, ymax=1)

ymin <- floor(min(all$lb)*100)/100
ymax <- ceiling(max(all$ub)*100)/100

p <- ggplot(data = all,aes(x = year,y = perc_yes,group= `Referendums in`,col = `Referendums in`))+
  geom_line()+
  geom_point()+
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha = 0.1,fill = "grey",colour = NA) +
  scale_y_continuous(name = "Share of ballot approved",labels = percent,lim = c(ymin,ymax))+
  geom_vline(xintercept = 2018.5, linetype = "dashed")+
  scale_x_continuous(name = "",breaks = seq(2008,2020,2))+
  scale_color_manual(values = c("black","#B31B1B"))+
  theme_minimal()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

ggsave(plot = p, file="Output/fig 1a.City_vs_sd_High_shock.png" , width=10, height=5,bg = "white")


##
low <- city[city$high_chg == 0,]

R <- lm(data = low,formula =  Pass ~ 0 + as.factor(year))
a <- as.data.frame(cbind(R$coefficients,confint(R,level = 0.9)))
colnames(a) <- c("perc_yes","lb","ub") 
a$year <- as.numeric(substr(rownames(a),16,19))


R <- lm(data = df[df$highpropchange ==0,],formula =  Pass ~ 0 + as.factor(year))
b <- as.data.frame(cbind(R$coefficients,confint(R,level = 0.9)))
colnames(b) <- c("perc_yes","lb","ub") 
b$year <- as.numeric(substr(rownames(b),16,19))

b$`Referendums in` <- "School districts"
a$`Referendums in` <- "Cities"

all <- rbind(a,b)

all <- all[all$year %%2 ==0,]

q <- ggplot(data = all,aes(x = year,y = perc_yes,group= `Referendums in`,col = `Referendums in`))+
  geom_line()+
  geom_point()+
  geom_ribbon(aes(ymin=lb, ymax=ub), alpha = 0.1,fill = "grey",colour = NA) +
  scale_y_continuous(name = "Share of ballot approved",labels = percent,lim = c(ymin,ymax))+
  geom_vline(xintercept = 2018.5, linetype = "dashed")+
  scale_x_continuous(name = "",breaks = seq(2008,2020,2))+
  scale_color_manual(values = c("black","#B31B1B"))+
  theme_minimal()+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())


ggsave(plot = q, file="Output/fig 1b.City_vs_sd_Low_shock.png" , width=10, height=5,bg = "white")


rm(list=setdiff(ls(), c("df","city")))
